﻿using AutoMapper;
using Prism.Ioc;
using Prism.Mvvm;
using FunasrSTT.Translate.ViewModels;
using FunasrSTT.Translate.Views;
using System.Configuration;
using System.Data;
using System.Resources;
using System.Windows;
using Prism.DryIoc;

namespace FunasrSTT.Translate;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
    /// <inheritdoc />
    protected override Window CreateShell() => Container.Resolve<Views.ShellView>();

    /// <inheritdoc />
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<ResourceDictionary>(() => Resources);
        containerRegistry.RegisterSingleton<IMapper>(CreateMapper);

        ViewModelLocationProvider.Register<Views.AlertDialog, ViewModels.AlertViewModel>();
        ViewModelLocationProvider.Register<Views.ConfirmDialog, ViewModels.ConfirmViewModel>();
        ViewModelLocationProvider.Register<Views.ShellView, ViewModels.ShellViewModel>();
        ViewModelLocationProvider.Register<Views.MainView, ViewModels.MainViewModel>();
        ViewModelLocationProvider.Register<Views.SettingView, ViewModels.SettingViewModel>();

    }

    /// <summary>
    /// 创建对象映射器.
    /// </summary>
    /// <returns>对象映射器.</returns>
    private static IMapper CreateMapper()
    {
        var configuration = new MapperConfiguration(c => c.AddProfile(typeof(Mappers.MyProfile)));
        return configuration.CreateMapper();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        //Task线程内未捕获异常处理事件
        TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常 

        //UI线程未捕获异常处理事件（UI主线程）
        this.DispatcherUnhandledException += App_DispatcherUnhandledException;

        //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

        base.OnStartup(e);
    }

    private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {
        try
        {
            var exception = e.Exception as Exception;
            if (exception != null)
            {
                HandleException(exception);
            }
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
        finally
        {
            e.SetObserved();
        }
    }

    //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)      
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        try
        {
            var exception = e.ExceptionObject as Exception;
            if (exception != null)
            {
                HandleException(exception);
            }
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
        finally
        {
            //ignore
        }
    }

    //UI线程未捕获异常处理事件（UI主线程）
    private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        try
        {
            HandleException(e.Exception);
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
        finally
        {
            e.Handled = true;
        }
    }

    private static void HandleException(Exception ex)
    {
        //记录日志
        Common.Helper.LogHelper.WriteErrorLog(ex);
    }
}
